﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data.SqlClient;
namespace WebApplication1
{
    public partial class Default : System.Web.UI.Page
    {
        public static String filePath1 = "";
        public static String filePath2 = "";
        public static String filePath3 = "";

        protected void Page_Load(object sender, EventArgs e)
        {
        }

        
        protected void file1Upload()
        {
            Boolean fileOK = false;
            String path = Server.MapPath("~/Uploaded/");
            if (FileUpload1.HasFile)
            {
                String fileExtension = System.IO.Path.GetExtension(FileUpload1.FileName).ToLower();
                String[] allowedExtensions = { ".txt", ".docx", ".doc", ".pdf", ".jpg" };
                for (int i = 0; i < allowedExtensions.Length; i++)
                {
                    if (fileExtension == allowedExtensions[i])
                    {
                        fileOK = true;
                    }
                }
            }

            if (fileOK)
            {
                    filePath1 = path + FileUpload1.FileName;
                    FileUpload1.SaveAs(filePath1);
                    //Label1.Text = "File uploaded!";
            }
            else
            {
                MessageBox("Cannot accept files of this type.");
            }
        }

        protected void file2Upload()
        {
            Boolean fileOK = false;
            String path = Server.MapPath("~/Uploaded/");
            if (FileUpload1.HasFile)
            {
                String fileExtension = System.IO.Path.GetExtension(FileUpload2.FileName).ToLower();
                String[] allowedExtensions = { ".txt", ".docx", ".doc", ".pdf", ".jpg" };
                for (int i = 0; i < allowedExtensions.Length; i++)
                {
                    if (fileExtension == allowedExtensions[i])
                    {
                        fileOK = true;
                    }
                }
            }

            if (fileOK)
            {
                    filePath2 = path + FileUpload2.FileName;
                    FileUpload2.SaveAs(filePath2);
                    //Label1.Text = "File uploaded!";
            }
            else
            {
                MessageBox("Cannot accept files of this type.");
            }
        }



        protected void Button1_Click(object sender, EventArgs e)
        {
            if (cc_name.Text == "" || cc_number.Text == "" || cc_ccv.Text == "")
                MessageBox("Please fill out all items in this form!");
            else
            {
                User user = new User();
                user.Name = name.Text;
                user.Email = email.Text;
                user.Subject = subject.Text;
                user.Grade = grade.Text;
                user.Files = FileUpload1.FileName;
                user.PaymentType = "card";
                user.CardName = cc_name.Text;
                user.CardNumber = cc_number.Text;
                user.ExpiryMonth = expiry_month.Text;
                user.ExpiryYear = expiry_year.Text;
                user.CCV = cc_ccv.Text;

                DBconnection db = new DBconnection();
                db.insertUser(user);

                if (FileUpload1.HasFile)
                {
                    file1Upload();
                    string token = ZenAPI.PostAttachment(filePath1);
                    if (FileUpload2.HasFile && Label.Text=="true")
                    {
                        file2Upload();
                        ZenAPI.PostAttachmentWithToken(filePath2,token);
                    }
                    string strXMLticket = "<ticket><subject>" + user.Subject + " - " + user.Grade + "</subject><description>Test</description><uploads>" + token + "</uploads><requester_email>" + user.Email + "</requester_email></ticket>";
                    ZenAPI.PostTicket(strXMLticket);
                }
                else
                {
                    string strXMLticket = "<ticket><subject>" + user.Subject + " - " + user.Grade + "</subject><description>Test</description><requester_email>" + user.Email + "</requester_email></ticket>";
                    ZenAPI.PostTicket(strXMLticket);
                }
                //String strXMLcomments = "<comment><is-public>true</is-public><value>this is a Test!</value><uploads></uploads></comment>";
                //String XMLuser = "<user><email>"+email+"</email><name>"+name+"</name><roles>4</roles><restriction-id>1</restriction-id></user>";
                //ZenAPI.CreateUser(XMLuser); 
                //ZenAPI.PostTicketcomment(strXMLcomments, "0");
                MessageBox("Your ticket has been submitted!");
                name.Text = "";
                email.Text = "";
                subject.Text = "";
                grade.Text = "";
                cc_name.Text = "";
                cc_number.Text = "";
                expiry_month.Text = "";
                expiry_year.Text = "";
                cc_ccv.Text = "";
            }
        }

        private void MessageBox(string message)
        {
            if (!string.IsNullOrEmpty(message))
            {
                this.ClientScript.RegisterClientScriptBlock(this.GetType(), "alt1", "alert('" + message + "');", true);
            }
        }

        protected void cancel1_Click(object sender, EventArgs e)
        {

        }

        protected void cancel2_Click(object sender, EventArgs e)
        {

        }

        protected void cancel3_Click(object sender, EventArgs e)
        {

        }

        protected void AttachNew_Click(object sender, EventArgs e)
        {
        }

    }
}